//
//  MyInforViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/5.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "MyInforViewController.h"
#import "CustomMyInfoTableViewCellOne.h"
#import "CustomMyInfoTableViewCellTwo.h"
#import "CustomSettingTableViewCellTwo.h"
#import "ChangeNicknameViewController.h"
@interface MyInforViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
    NSArray *titleArray;
    CustomMyInfoTableViewCellOne* headCell;
}

@end

@implementation MyInforViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleArray=[NSArray arrayWithObjects:@"账号",@"昵称",@"性别",@"",@"我的邀请人", nil];

    [self setNaviTitle:@"我"];
    [self setNaviLeftButton:@""];
    [self.view addSubview:self.displayView];
        // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        // [_displayView setSeparatorColor:[UIColor redColor]];
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    switch (indexPath.row) {
        case 0:
        {
            static NSString *identifier = @"CustomMyInfoTableViewCellOne";
            headCell = [tableView dequeueReusableCellWithIdentifier:
                                                    identifier];
            if (headCell == nil) {
                headCell = [[[NSBundle mainBundle] loadNibNamed:@"CustomMyInfoTableViewCellOne"  owner:self options:nil] lastObject];
                
            }
            [headCell.headImageView sd_setImageWithURL:[NSURL URLWithString:APP_CONTEXT.account.data.avatar] placeholderImage:nil];
            headCell.selectionStyle=UITableViewCellSelectionStyleNone;
            return headCell;
            
        }
            break;
        case 1:
        case 2:
        case 3:
        case 5:
        {
            static NSString *identifier = @"CustomMyInfoTableViewCellTwo";
            CustomMyInfoTableViewCellTwo* cell = [tableView dequeueReusableCellWithIdentifier:
                                                   identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomMyInfoTableViewCellTwo"  owner:self options:nil] lastObject];
                
            }
            cell.nameLabel.text=titleArray[indexPath.row-1];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            if (indexPath.row==3 ||indexPath.row==5) {
                [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            }
            if (indexPath.row==1) {
                cell.contentLabel.text=APP_CONTEXT.account.data.telephone;
            }
            if (indexPath.row==2) {
                cell.contentLabel.text=APP_CONTEXT.account.data.firstname;
            }
            if (indexPath.row==3) {
                switch (APP_CONTEXT.account.data.gender) {
                    case 0:
                      cell.contentLabel.text=@"公主";
                        break;
                    case 1:
                       cell.contentLabel.text=@"男神";
                        break;
                    default:
                        break;
                }
               
            }
            return cell;
        }
            break;
        case 4:
        {
            static NSString *identifier = @"CustomSettingTableViewCellTwo";
            CustomSettingTableViewCellTwo* cell = [tableView dequeueReusableCellWithIdentifier:
                                                   identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomSettingTableViewCellTwo"  owner:self options:nil] lastObject];
                
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
            
        }
            break;
        default:
            return nil;
            break;
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 4:
            return 5;
            break;
            
        default:
            return 44;
            break;
    }
   
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {
            //头像
            UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                          initWithTitle:@"更换头像"
                                          delegate:self
                                          cancelButtonTitle:@"取消"
                                          destructiveButtonTitle:nil
                                          otherButtonTitles:@"拍照",@"从相册中选择",nil];
            actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            actionSheet.tag=100;
            [actionSheet showInView:self.view];
        }
            break;
        case 1:
        {
            //账号
            
        }
            break;
        case 2:
        {
            //昵称
            ChangeNicknameViewController *nickVC=[[ChangeNicknameViewController alloc] init];
            nickVC.vcDelegate=self;
            [self.navigationController pushViewController: nickVC animated:YES];
        }
            break;
        case 3:
        {
            //性别
            UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                          initWithTitle:@"选择性别"
                                          delegate:self
                                          cancelButtonTitle:@"取消"
                                          destructiveButtonTitle:nil
                                          otherButtonTitles:@"公主",@"男神",nil];
            actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            actionSheet.tag=101;
            [actionSheet showInView:self.view];

        }
            break;
        case 5:
        {
            //我的邀请人
            ChangeNicknameViewController *nickVC=[[ChangeNicknameViewController alloc] init];
            nickVC.isInviter=YES;
            nickVC.vcDelegate=self;
            [self.navigationController pushViewController: nickVC animated:YES];
            
        }
            break;
        default:
            break;
    }
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.cancelButtonIndex == buttonIndex) {
        return;
    }
    switch (actionSheet.tag) {
        case 100:
        {
            NSUInteger sourceType = 0;
            // 判断是否支持相机
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                switch (buttonIndex) {
                    case 0:    // 相机
                        sourceType = UIImagePickerControllerSourceTypeCamera;
                        break;
                    case 1:    // 相册
                        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                        break;
                }
            }else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
            // 跳转到相机或相册页面
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = YES;
            imagePickerController.sourceType = sourceType;
            if([[[UIDevice
                  currentDevice] systemVersion] floatValue]>=8.0) {
                
                self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
                
            }
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }
            break;
        case 101:
        {
            
            [self changeGender:buttonIndex];
            
        }
            break;
        default:
            break;
    }
    

}
-(void)reloadNickName:(NSString *)nickName{
    NSIndexPath *index = [NSIndexPath indexPathForRow:2 inSection:0];
    CustomMyInfoTableViewCellTwo *cell=[_displayView cellForRowAtIndexPath:index];
    cell.contentLabel.text=nickName;
}
#pragma mark-修改性别
-(void)changeGender:(int)gender{
    [[LKDHud shareHud] showLoadingView];
    [ApiClient changeGender:[NSString stringWithFormat:@"%d",gender] didFinishLoaded:^(KXJson *json, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        
        if ([[json getStringForKey:@"status"] intValue]==1) {
            NSArray *arr=[NSArray arrayWithObjects:@"公主",@"男神", nil];
            NSIndexPath *index = [NSIndexPath indexPathForRow:3 inSection:0];
            CustomMyInfoTableViewCellTwo *cell=[_displayView cellForRowAtIndexPath:index];
            cell.contentLabel.text=arr[gender];
        }
    
    } didFailLoaded:^(NSError *error, NSString *path) {
       [[LKDHud shareHud] hideLoadingView];
        
    }];
    
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    __block UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imgData = UIImageJPEGRepresentation(image, 0.5);
    NSString *aPath=[NSString stringWithFormat:@"%@/Documents/head.png",NSHomeDirectory()];
    [imgData writeToFile:aPath atomically:YES];
    [[LKDHud shareHud] showLoadingView];
    [ApiClient upLoadHeadImage:aPath didFinishLoaded:^(KXJson *json, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        if ([[json getStringForKey:@"status"] intValue]==1) {
            KXJson *dataJson=[json getJsonForKey:@"data"];
            [headCell.headImageView sd_setImageWithURL:[NSURL URLWithString:[dataJson getStringForKey:@"avatar"]]];
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

@end
